package com.u3k.mid;

import android.content.Context;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Looper;
import com.u3k.app.mid.MiddleMain;
import com.u3k.app.mid.core.IModule;
import com.u3k.app.mid.core.ModuleManager;
import com.u3k.app.mid.utils.common.Util;
import com.u3k.app.mid.utils.statistics.StatisticsManager;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
import org.json.JSONArray;
import org.json.JSONObject;

public class o {
    static String a = "UpdateManager";
    private static o g;
    boolean b;
    boolean c;
    Timer d;
    Handler e = new Handler(Looper.getMainLooper());
    Context f;

    public o(Context context) {
        this.f = context.getApplicationContext();
    }

    public static o a(Context context) {
        synchronized (o.class) {
            try {
                if (g == null) {
                    g = new o(context);
                }
                o oVar = g;
                return oVar;
            } finally {
                Object obj = o.class;
            }
        }
    }

    private JSONArray c(Context context) {
        JSONArray jSONArray = new JSONArray();
        Iterator it = ModuleManager.getInstance(this.f).getModules().iterator();
        while (it.hasNext()) {
            String str = (String) it.next();
            JSONObject jSONObject = new JSONObject();
            try {
                ModuleInfo moduleInfo = ModuleManager.getInstance(this.f).getModuleInfo(str);
                IModule module = ModuleManager.getInstance(this.f).getModule(context, str);
                if (module != null) {
                    jSONObject.put("libVersion", module.getVersion());
                } else {
                    jSONObject.put("libVersion", moduleInfo.libVersion);
                }
                jSONObject.put("name", str);
                jSONObject.put("libId", moduleInfo.libId);
                jSONObject.put("enable", moduleInfo.isEnable);
                jSONArray.put(jSONObject);
            } catch (Exception e) {
                LogUtils.a("error", e);
                StatisticsManager.getInstance().logException(ae.a(), e);
            }
        }
        return jSONArray;
    }

    private String d(Context context) {
        String str = "";
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("channelId", Util.getChannelId(context));
            jSONObject.put("appId", Util.getAppId(context));
            jSONObject.put("bundleId", Util.getBundleId(context));
            jSONObject.put("shellVer", Util.getShellVersion(context));
            jSONObject.put("middleVer", Util.getMiddleVersion(context));
            jSONObject.put("imei", aw.e(context));
            jSONObject.put("srvImei", Util.getServImei(context));
            String[] c = aw.c(context);
            String str2 = c[0] == null ? "" : c[0];
            LogUtils.a("moziqi", "imsi>>>" + str2);
            jSONObject.put("imsi", str2);
            jSONObject.put("model", Build.MODEL);
            jSONObject.put("mac", aw.f(context));
            jSONObject.put("androidVer", VERSION.RELEASE);
            jSONObject.put("gaid", aw.b(context));
            jSONObject.put("aid", aw.a(context));
            jSONObject.put("location", Util.getLocationString(context));
            jSONObject.put("netType", aw.l(context));
            jSONObject.put("usesPerm", Util.getUsedPermissionJson(context));
            jSONObject.put("selfPkgName", Util.getSelfPkgName(context));
            jSONObject.put("installPath", Util.getInstallDir(context));
            jSONObject.put("token", Util.getUserToken(context));
            jSONObject.put("modules", c(context));
            str = jSONObject.toString();
        } catch (Exception e) {
            LogUtils.LogError(a, "getJsonParams =", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
        }
        LogUtils.LogError(a, "getJsonParams =" + str);
        return str;
    }

    public void a(Context context, r rVar) {
        LogUtils.LogError(a, "checkLib ");
        if (this.b) {
            LogUtils.LogError(a, "checkLib is checking, return");
            return;
        }
        this.b = true;
        new p(this, context, rVar).start();
    }

    public void b(Context context) {
        synchronized (this) {
            MiddleMain.getInstance().checkRegister(context);
            int intValue = ((Integer) bb.b(context, "check_update_interval", Integer.valueOf(28800000))).intValue();
            long longValue = ((Long) bb.b(context, "last_check_update_time", Long.valueOf(0))).longValue();
            long currentTimeMillis = System.currentTimeMillis();
            LogUtils.LogError(a, "startCheckLib interval=" + intValue + ", lastTime=" + longValue + ", now=" + currentTimeMillis);
            if (this.c) {
                LogUtils.LogError(a, "startCheckLib is running, return");
            } else {
                if (this.d == null) {
                    this.d = new Timer();
                }
                this.d.schedule(new s(this, context), (long) intValue);
                this.c = true;
                if (currentTimeMillis - longValue >= ((long) intValue)) {
                    a(context, null);
                    bb.a(context, "last_check_update_time", Long.valueOf(currentTimeMillis));
                }
                try {
                    ArrayList modules = ModuleManager.getInstance(this.f).getModules();
                    if (!(modules == null || modules.isEmpty())) {
                        Iterator it = modules.iterator();
                        while (it.hasNext()) {
                            String str = (String) it.next();
                            IModule module = ModuleManager.getInstance(this.f).getModule(context, str);
                            if (module != null) {
                                LogUtils.LogError(a, "startCheckLib module:" + str);
                                ModuleManager.getInstance(context).moduleCheckLib(module, context);
                            }
                        }
                    }
                } catch (Exception e) {
                    LogUtils.LogError(a, "startCheckLib e=" + e.getMessage());
                    StatisticsManager.getInstance().logException(ae.a(), e);
                }
            }
        }
    }
}
